package com.example.powernodemall.service.impl;

import com.example.powernodemall.ProjectEnum.ResultEnum;
import com.example.powernodemall.constant.BaseConstant;
import com.example.powernodemall.service.SecurityDetailService;
import com.example.powernodemall.strategy.LoginOption;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

@Component
public class SecurityDetailServiceImpl implements SecurityDetailService {
    @Resource
    LoginOption loginOption;
    @Override
    public UserDetails loadUserByUsername(String username)  {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        String loginType = request.getHeader(BaseConstant.LOGIN_TYPE);
        if (loginType == null) {
            throw new RuntimeException(ResultEnum.NOT_FOND.getMsg());
        }
        return loginOption.getLoginMethod(loginType).loadUserByUsername(username);
    }
}
